<?php
function displayAddLectureForm($idFormation) {
    global $babBody;

    class displayAddLectureFormClass {

        function displayAddLectureFormClass($idFormation) {

            $jquery = bab_functionality::get('jquery');
            if ($jquery !== false) {
                $jquery->includeCore();
                $jquery->includeUI();
            }

            $this->idFormation = $idFormation;

            /* LABELS */
            $this->classroomLabel = bab_translate("Classroom", $GLOBALS['babAddonFolder']);
            $this->materialLabel = bab_translate("Material", $GLOBALS['babAddonFolder']);
            $this->lectureTypeLabel = bab_translate("Type of Lecture", $GLOBALS['babAddonFolder']);
            $this->dateLabel = bab_translate("Date", $GLOBALS['babAddonFolder']);
            $this->hoursLabel = bab_translate("Hours", $GLOBALS['babAddonFolder']);
            $this->lengthLabel = bab_translate("Length", $GLOBALS['babAddonFolder']);
            $this->tutorialGroupLabel = bab_translate("Tutorial Group", $GLOBALS['babAddonFolder']);
            $this->workPraticeGroupLabel = bab_translate("Work Pratice Group", $GLOBALS['babAddonFolder']);

            /* BUTTONS */
            $this->addLectureButton = bab_translate("Add Lecture", $GLOBALS['babAddonFolder']);


            /* LIST */
            $this->lectureTypeList = unsCalendar_getAllLectureType();
            $this->lectureTypeListLength = count($this->lectureTypeList);

            $this->semesterList = unsDepartement_getSemesterForFormation($idFormation);

            $this->teachingUnitS1List = unsDepartement_getTeachingUnitsForSemester($this->semesterList[0]['id']);
            $this->teachingUnitS1ListLength = count($this->teachingUnitS1List);

            $this->teachingUnitListS2 = unsDepartement_getTeachingUnitsForSemester($this->semesterList[1]['id']);
            $this->teachingUnitListS2 = count($this->teachingUnitListS2);

            $this->tutorialGroupsList = unsDepartement_getTutorialGroupsForFormation($idFormation);
            $this->tutorialGroupsListLength = count($this->tutorialGroupsList);
            
            $this->buildingList = unsCalendar_getAllBuilding();
            $this->buildingListLength = count($this->buildingList);

        }
        
        function nextBuilding()
        {
            static $i = 0;
            if($i < $this->buildingListLength)
            {
                $this->building = $this->buildingList[$i];
                
                $this->classroomList = unsCalendar_getClassroomForBuilding($this->building['id']);
                $this->classroomListLength = count($this->classroomList);
                $i++;
                return true;
            } else {
                return false;
            }
        }
        
        function nextClassroom()
        {
            static $i = 0;
            if($i < $this->classroomListLength)
            {
                $this->classroom = $this->classroomList[$i];
                $i++;
                return true;
            } else
                return false;
        }
        
        function nextLectureType()
        {
            static $i = 0;

            if ($i < $this->lectureTypeListLength) {
                $this->lectureType = $this->lectureTypeList[$i];

                $i++;
                return true;
            } else {
                return false;
            }
        }

        function nextMaterialS1()
        {
            static $i = 0;

            if ($i < $this->materialS1ListLength) {
                $this->materialS1 = $this->materialS1List[$i];

                $i++;
                return true;
            } else {
                return false;
            }
        }

        function nextTeachingUnitS1()
        {
            static $i = 0;

            if ($i < $this->teachingUnitS1ListLength) {
                $this->teachingUnitS1 = $this->teachingUnitS1List[$i];

                $this->materialS1List = unsDepartement_getMaterialsForTeachingUnit($this->teachingUnitS1['id']);
                $this->materialS1ListLength = count($this->materialS1List);

                $i++;
                return true;
            } else {
                return false;
            }
        }

        function nextTutorialGroup()
        {
            static $i = 0;

            if ($i < $this->tutorialGroupsListLength) {
                $this->tutorialGroup = $this->tutorialGroupsList[$i];

                $this->workPraticeGroupList = unsDepartement_getWorkPraticeGroupsForTutorialGroup($this->tutorialGroup['id']);
                $this->workPraticeGroupListLength = count($this->workPraticeGroupList);

                $i++;
                return true;
            } else {
                $i = 0;
                return false;
            }
        }

        function nextWorkPraticeGroup()
        {
            static $i = 0;

            if ($i < $this->workPraticeGroupListLength) {
                $this->workPraticeGroup = $this->workPraticeGroupList[$i];

                $i++;
                return true;
            } else {
                return false;
            }
        }

        function nextHour()
        {
            static $i = 7;

            if ($i < 21) {
                
                $this->hour = $i;

                $i++;
                return true;
            } else {
                $i = 0;
                return false;
            }
        }
        
        function nextMin()
        {
            static $i = 0;

            if ($i < 60) {
                
                $this->min = $i;

                $i += 15;
                return true;
            } else {
                $i = 0;
                return false;
            }
        }
        
        function nextLengthHour()
        {
            static $i = 0;
            if($i < 10)
            {
                $this->lengthHour = $i;
                $i++;
                return true;
            } else
                return false;
        }
        
        function nextLengthMin()
        {
            static $i = 0;
            if($i < 60)
            {
                $this->lengthMin = $i;
                $i+=15;
                return true;
            } else
                return false;
        }
    }

    $obj = new displayAddLectureFormClass($idFormation);
    $babBody->babecho(bab_printTemplate($obj, $GLOBALS['babAddonHtmlPath'] . "lectureUser.html", "add"));
}

function displayListLecture($idFormation) {
    global $babBody;

    class displayListLectureClass {

        function displayListLectureClass($idFormation) {
            
            /* LABELS */
            $this->lectureDateLabel = bab_translate("Lecture Date", $GLOBALS['babAddonFolder']);
            $this->materialLabel = bab_translate("Material", $GLOBALS['babAddonFolder']);
            $this->lectureTypeLabel = bab_translate("Lecture Type", $GLOBALS['babAddonFolder']);
            $this->tGLabel = bab_translate("Tutorial Group", $GLOBALS['babAddonFolder']);
            $this->wPGLabel = bab_translate("WP Group", $GLOBALS['babAddonFolder']);
            $this->lengthLabel = bab_translate("Length", $GLOBALS['babAddonFolder']);
            
            $this->formation = unsDepartement_getFormation($idFormation);

            $this->semestersList = unsDepartement_getSemesterForFormation($idFormation);
            $this->semestersListLength = count($this->semestersList);
            
            $this->lecturesList = unsCalendar_getLecturesForFormation($idFormation);
            $this->lecturesListLength = count($this->lecturesList);
          

        }

        function nextSemester() {
            static $k = 0;
            if ($k < $this->semestersListLength) {

                $this->semester = $this->semestersList[$k];
                $k++;
                return true;
            } else {
                return false;
            }
        }
        
        function nextLecture()
        {
            static $i = 0;
            if($i < $this->lecturesListLength)
            {
                $this->lecture = $this->lecturesList[$i];
                $this->material = unsDepartement_getMaterial($this->lecture['lecture_materialID']);
                $this->lectureType = unsCalendar_getLectureType($this->lecture['lecture_lectureTypeID']);
                
                if($this->lecture['lecture_tutorialGroupID'] == 0)
                {
                $this->tGroup['name'] = '';
                $this->wPGroup = unsDepartement_getWorkPraticeGroup($this->lecture['lecture_workPraticeGroupID']);
                } else {
                     $this->tGroup = unsDepartement_getTutorialGroup($this->lecture['lecture_tutorialGroupID']);
                $this->wPGroup['name'] = '';
                }
                
                $i++;
                return true;
            } else {
                return false;
            }
        }

    }

    $obj = new displayListLectureClass($idFormation);
    $babBody->babecho(bab_printTemplate($obj, $GLOBALS['babAddonHtmlPath'] . "lectureUser.html", "list"));
}

function displaySelectFormation() {
    global $babBody;

    class displaySelectFormationClass {

        function displaySelectFormationClass() {
            /* LABELS */
            $this->selectFormationLabel = bab_translate("Select Formation", $GLOBALS['babAddonFolder']);

            /* BUTTONS */
            $this->submitButton = bab_translate("Select", $GLOBALS['babAddonFolder']);

            $this->departmentsList = unsDepartement_getAllDepartments();
            $this->departmentsListLength = count($this->departmentsList);
        }

        function nextDepartment() {
            static $i = 0;
            if ($i < $this->departmentsListLength) {
                $this->department = $this->departmentsList[$i];

                $this->formationsList = unsDepartement_getFormationsForDepartement($this->department['id']);
                $this->formationsListLength = count($this->formationsList);

                $i++;
                return true;
            } else {
                return false;
            }
        }

        function nextFormation() {
            static $i = 0;
            if ($i < $this->formationsListLength) {
                $this->formation = $this->formationsList[$i];
                $i++;
                return true;
            } else {
                return false;
            }
        }

    }

    $obj = new displaySelectFormationClass();
    $babBody->babecho(bab_printTemplate($obj, $GLOBALS['babAddonHtmlPath'] . "lectureUser.html", "selectFormation"));
}

$idFormation = bab_rp('fid');

global $babBody;

if ($idFormation == null) {
    $babBody->addItemMenu("selectFormation", bab_translate("Select Formation", $GLOBALS['babAddonFolder']), $GLOBALS['babAddonUrl'] . "user&idx=lecture&do=selectFormation");

    $do = "selectFormation";
    displaySelectFormation();
} else {
    
    if(isset($_POST['addLectureSubmit']))
    {
        if(null != $_POST['materialID'] 
                && null != $_POST['lectureTypeID']
                && null != $_POST['startDate'] 
                && null != $_POST['startHour'] 
                && null != $_POST['startMin']
                && null != $_POST['lengthHour'] 
                && null != $_POST['lengthMin']
                && null != $_POST['classroomID'])
        {
            unsCalendar_addLecture($_POST['materialID'], 
                    $_POST['lectureTypeID'], 
                    $idFormation, 
                    $_POST['tutorialGroupID'], 
                    $_POST['workPraticeGroupID'],
                    $_POST['classroomID'],
                    $_POST['startDate'],
                    $_POST['startHour'],
                    $_POST['startMin'],
                    $_POST['lengthHour'],
                    $_POST['lengthMin'], 1);
        }
        else {
            echo 'e';
        }
    }


    $babBody->addItemMenu("listLecture", bab_translate("Lectures List", $GLOBALS['babAddonFolder']), $GLOBALS['babAddonUrl'] . "user&idx=lecture&fid=$idFormation&do=listLecture");
    $babBody->addItemMenu("addLecture", bab_translate("Add Lecture", $GLOBALS['babAddonFolder']), $GLOBALS['babAddonUrl'] . "user&idx=lecture&fid=$idFormation&do=addLecture");
    $babBody->addItemMenu("createCalendar", bab_translate("Create Calendar", $GLOBALS['babAddonFolder']), $GLOBALS['babAddonUrl'] . "user&idx=lecture&fid=$idFormation&do=createCalendar");

    $do = bab_rp('do', 'listLecture');

    switch ($do) {
        case "addLecture":
            //if (bab_isAccessValid('unsDepartement_rights_groups', 1000)) {
            $babBody->title = bab_translate("Add Lecture", $GLOBALS['babAddonFolder']);
            displayAddLectureForm($idFormation);
            break;
        // }
        case "createCalendar":
            unsCalendar_createCalendarsForFormation($idFormation);
            break;

        case "listLecture":
            $babBody->title = bab_translate("Lecture list", $GLOBALS['babAddonFolder']);
            displayListLecture($idFormation);
            break;
    }
}

$babBody->setCurrentItemMenu($do);
?>
